package com.uuah.server.config.spring.parsers.specific;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.SystemPropertyUtils;
import org.w3c.dom.Element;

import com.uuah.server.config.spring.parsers.assembly.IBeanAssembler;
import com.uuah.server.config.spring.parsers.generic.UuahOrphanDefinitionParser;

public class GlobalPropertyDefinitionParser extends UuahOrphanDefinitionParser {
	private static Logger logger = LoggerFactory
			.getLogger(GlobalPropertyDefinitionParser.class);
	private final String VALUE_ATTR = "value";
	private final String NAME_ATTR = "name";

	public GlobalPropertyDefinitionParser() {
		super(true);
		addIgnored(NAME_ATTR);
		addIgnored(VALUE_ATTR);
	}

	protected Class getBeanClass(Element element) {
		return String.class;
	}

	protected void postProcess(ParserContext context, IBeanAssembler assembler,
			Element element) {
		String name = element.getAttribute(NAME_ATTR);
		if (name.indexOf(' ') != -1) {
			logger
					.warn("Environment property name should not contain spaces: \""
							+ name + "\"");
		}

		String value = element.getAttribute(VALUE_ATTR);
		assembler.getBean().addConstructorArgValue(
				SystemPropertyUtils.resolvePlaceholders(value));
		super.postProcess(context, assembler, element);
	}
}
